package com.sunwayhorizo.health.hl7.persistent;

import com.sunwayhorizo.health.hl7.datatypes.common.ADXPImpl;
import com.sunwayhorizo.health.hl7.datatypes.common.CSImpl;
import com.sunwayhorizo.health.hl7.persistent.pk.RimCoreEntyAddrsPK;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;


@Entity
//@TableGenerator(name="EntyAddrs_IdGenerator", table="RIM_Id_Generator", pkColumnName = "pkColumn", valueColumnName = "valueColumn",pkColumnValue="Enty_Addrs")
//@NamedQuery(name = "EntyAddrs.findAll", query = "select o from EntyAddrs o")
@Table(name = "RIM_CORE_ENTY_ADDRS")
@IdClass(RimCoreEntyAddrsPK.class)
public class RimCoreEntyAddrs implements Serializable {
    @Id
    @Column(name="ENTITY_ID", nullable = false, insertable = false, updatable = false)    private Long entityId;
    @Id
    @Column(name="ENTITY_VERSION_NUM", nullable = false, insertable = false, updatable = false)     private Long entityVersionNum;
    @Id
//    @GeneratedValue(strategy = GenerationType.TABLE,generator = "EntyAddrs_IdGenerator" )
    @Column(name="ADDRESS_ID", nullable = false)    private Long addressId;
    
    @Column(name="EFFECTIVE_START_DATE_TS")    private Timestamp effectiveStartDateTs;
    @Column(name="EFFECTIVE_END_DATE_TS")    private Timestamp effectiveEndDateTs;    
    @Column(name="EFFECTIVE_GTS_TXT")    private String effectiveGtsTxt;
    
    @Column(name="PARTY_SITE_ID")    private Long partySiteId;
    @Column(name="ADDRESS_TXT")    private String formatted;
    
    @OneToMany(mappedBy = "rimCoreEntyAddrs",cascade = CascadeType.ALL,fetch = FetchType.LAZY)   
    private List<RimCoreEntyAddrParts> entyeAddrPartsList = new ArrayList();
    
    @OneToMany(mappedBy = "rimCoreEntyAddrs",cascade = CascadeType.ALL,fetch = FetchType.LAZY)   
    private List<RimCoreEntyAddrUses> entyAddrUsesList = new ArrayList();
    
    @ManyToOne
    @JoinColumns({
    @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID"),
    @JoinColumn(name = "ENTITY_VERSION_NUM", referencedColumnName = "ENTITY_VERSION_NUM")
    })
    private RimCoreEntities rimCoreEntities;

    public RimCoreEntyAddrs() {
    }

    public Long getAddressId() {
        return addressId;
    }

    public void setAddressId(Long addressId) {
        this.addressId = addressId;
    }

    public Timestamp getEffectiveEndDateTs() {
        return effectiveEndDateTs;
    }

    public void setEffectiveEndDateTs(Timestamp effectiveEndDateTs) {
        this.effectiveEndDateTs = effectiveEndDateTs;
    }

    public String getEffectiveGtsTxt() {
        return effectiveGtsTxt;
    }

    public void setEffectiveGtsTxt(String effectiveGtsTxt) {
        this.effectiveGtsTxt = effectiveGtsTxt;
    }

    public Timestamp getEffectiveStartDateTs() {
        return effectiveStartDateTs;
    }

    public void setEffectiveStartDateTs(Timestamp effectiveStartDateTs) {
        this.effectiveStartDateTs = effectiveStartDateTs;
    }

    public Long getEntityId() {
        return entityId;
    }

    public void setEntityId(Long entityId) {
        this.entityId = entityId;
    }

    public Long getEntityVersionNum() {
        return entityVersionNum;
    }

    public void setEntityVersionNum(Long entityVersionNum) {
        this.entityVersionNum = entityVersionNum;
    }

    public Long getPartySiteId() {
        return partySiteId;
    }

    public void setPartySiteId(Long partySiteId) {
        this.partySiteId = partySiteId;
    }


    public void setFormatted(String formatted) {
        this.formatted = formatted;
    }

    public String getFormatted() {
        return formatted;
    }
    
    public void setEntyeAddrPartsList(List<RimCoreEntyAddrParts> entyeAddrPartsList) {
        this.entyeAddrPartsList = entyeAddrPartsList;
    }

    public List<RimCoreEntyAddrParts> getEntyeAddrPartsList() {
        return entyeAddrPartsList;
    }
    
    private RimCoreEntyAddrParts addRimCoreEntyAddrParts(RimCoreEntyAddrParts entyAddrParts) {
        this.getEntyeAddrPartsList().add(entyAddrParts);
        entyAddrParts.setRimCoreEntyAddrs(this);
        return entyAddrParts;
    }

    private RimCoreEntyAddrParts removeRimCoreEntyAddrParts(RimCoreEntyAddrParts entyAddrParts) {
        this.getEntyeAddrPartsList().remove(entyAddrParts);
        entyAddrParts.setRimCoreEntyAddrs(null);
        return entyAddrParts;
    }
    
    public void setEntyAddrParts(List mList){
        for (int i = 0 ;i < mList.size() ; i++ )  {
            ADXPImpl adxp = (ADXPImpl)mList.get(i);
            RimCoreEntyAddrParts entyAddrParts = new RimCoreEntyAddrParts();
            entyAddrParts.setPartOrderNum(new Long(i));
            entyAddrParts.setPartType(adxp.partType().toString());
            entyAddrParts.setLiteral(adxp.literal().toString());
            this.addRimCoreEntyAddrParts(entyAddrParts);        
        }    
    }
    
    public void setEntyAddrUsesList(List<RimCoreEntyAddrUses> entyAddrUsesList) {
        this.entyAddrUsesList = entyAddrUsesList;
    }

    public List<RimCoreEntyAddrUses> getEntyAddrUsesList() {
        return entyAddrUsesList;
    }
    
    private RimCoreEntyAddrUses addRimCoreEntyAddrUses(RimCoreEntyAddrUses entyAddrUses) {
        this.getEntyAddrUsesList().add(entyAddrUses);
        entyAddrUses.setRimCoreEntyAddrs(this);
        return entyAddrUses;
    }

    private RimCoreEntyAddrUses removeRimCoreEntyAddrUses(RimCoreEntyAddrUses entyAddrUses) {
        this.getEntyAddrUsesList().remove(entyAddrUses);
        entyAddrUses.setRimCoreEntyAddrs(null);
        return entyAddrUses;
    }
    
    public void setEntyAddrUses(Set mSet){
        CSImpl use = (CSImpl)mSet.iterator().next();
        RimCoreEntyAddrUses entyAddrUses = new RimCoreEntyAddrUses();
        entyAddrUses.setUse(use.code().toString());
        this.addRimCoreEntyAddrUses(entyAddrUses);            
    }

    public void setRimCoreEntities(RimCoreEntities rimCoreEntities) {
        this.rimCoreEntities = rimCoreEntities;
    }

    public RimCoreEntities getRimCoreEntities() {
        return rimCoreEntities;
    }

}
